package com.atguigu.spzx.manager.handler;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.atguigu.spzx.manager.mapper.CategoryMapper;
import com.atguigu.spzx.model.dto.product.CategoryExcelDTO;
import com.atguigu.spzx.model.vo.product.CategoryExcelVo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LT
 * @create 2024-03-17-11:12
 */
public class CategoryExcelListener extends AnalysisEventListener<CategoryExcelDTO> {

    CategoryMapper categoryMapper;

    private List<CategoryExcelDTO> list = new ArrayList<>();
    private int limit = 100;

    public CategoryExcelListener(CategoryMapper categoryMapper) {
        this.categoryMapper = categoryMapper;
    }

    @Override
    public void invoke(CategoryExcelDTO categoryExcelDTO, AnalysisContext analysisContext) {
        Long count = categoryMapper.selectCountCondition(categoryExcelDTO);
        if (count > 0){
            return;
        }
        list.add(categoryExcelDTO);
        if (list.size()>=limit){
            categoryMapper.saveBatch(list);
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (list.size()>0){
            categoryMapper.saveBatch(list);
            list.clear();
        }
    }
}
